package com.tanhua.dubbo.api.mongo;

import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.ContactVo;
import com.tanhua.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class FriendApiImpl implements FriendApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public PageResult<Friend> findPageResult(Long page, Long pagesize, Long longInUserId) {
        Query query = new Query(Criteria.where("userId").is(longInUserId));
        long total = mongoTemplate.count(query, Friend.class);
        List<Friend> friendList=new ArrayList<>();
        if (total>0){
            query.skip((page-1)*pagesize).limit(pagesize.intValue());
            query.with(Sort.by(Sort.Order.desc("created")));
            friendList = mongoTemplate.find(query, Friend.class);
        }
        return PageResult.pageResult(page,pagesize, friendList,total);
    }
}
